{extend name="../../base/view/common/base" /}
{block name="style"}
<style>
.hover-view i,.hover-edit i{margin-left:4px; font-weight:800; cursor:pointer; opacity:0}
.hover-edit.hover-on i{opacity:1}
.content-content{line-height:1.6;}
.content-content p{margin-bottom:6px;}
.content-content img{max-width:100%; padding:8px; box-sizing: border-box;}

.comment-input .comment-image{width:40px; height:40px; border-radius:50%}
.comment-item .comment-avatar{width:50px; float:left}
.comment-item .comment-image{width:36px; height:36px; border-radius:50%}
.comment-item .comment-body{margin-left:50px;}
.comment-item .comment-content blockquote{border-left:3px solid #f1f1f1; padding:4px 8px;}
.comment-item .comment-actions a{color:#8c95a8; cursor:pointer; font-size:12px;}
.comment-item .comment-actions a:hover{color:#3582fb;}
.comment-meta span{font-size:12px;}

.log-item i{font-weight:800; color:#323232}
.log-content strong{margin:0 4px; color:#323232}
.layui-unselect dl {max-height:188px;}
</style>
{/block}
{block name="body"}
<div>
	<div class="p-3 border-b">
		<h2 class="pb-3 hover-edit">
			<span class="font20" id="title_{$detail.id}" data-val="">{$detail.title}</span>
			<i class="iconfont icon-wodedianping" title="编辑" data-name="title"></i>
		</h2>
		<div>
			<span class="layui-badge layui-bg-gray">#P{$detail.id}</span>
			<span class="mx-2">{$detail.admin_name}</span>
			<span class="gray">创建于{$detail.times}<span id="editTips">{gt name="$detail.update_time" value="0"}，最近更新于 {:time_trans($detail.update_time)}{/gt}</span></span>
			{eq name="$detail.admin_id" value="$login_admin"}
			<span class="layui-btn layui-btn-xs layui-btn-danger ml-4" id="delBtn"><i class="layui-icon">&#xe640;</i>删除</span>
			{/eq}
		</div>
	</div>
	<div class="layui-row border-b">
		<div class="layui-col-md9 p-3 border-r">
			<table class="layui-table layui-table-form">
				<tr>
					<td class="layui-td-gray">任务负责人</td>
					<td class="hover-edit"><span id="director_uid_{$detail.id}"
							data-val="{$detail.director_uid}">{$detail.director_name}</span><i
							class="iconfont icon-wodedianping" title="编辑" data-name="director_uid"></i></td>
					<td class="layui-td-gray">协作人</td>
					<td class="hover-edit" colspan="5"><span id="assist_admin_ids_{$detail.id}"
							data-val="{$detail.assist_admin_ids}">{$detail.assist_admin_names}</span><i
							class="iconfont icon-wodedianping" title="编辑" data-name="assist_admin_ids"></i></td>
					</td>
				</tr>
				<tr>
					<td class="layui-td-gray-2">计划完成日期</td>
					<td class="hover-edit"><span id="end_time_{$detail.id}" data-val="">{$detail.end_time|
							date='Y-m-d'}</span>{gt name="$detail.delay" value="0"}<span class="layui-badge ml-2">逾期
							{$detail.delay} 天</span>{/gt}<i class="iconfont icon-wodedianping" title="编辑"
							data-name="end_time"></i></td>
					<td class="layui-td-gray">预估工时</td>
					<td class="hover-edit"><span id="plan_hours_{$detail.id}" data-val="">{$detail.plan_hours}</span><i
							class="iconfont icon-wodedianping" title="编辑" data-name="plan_hours"></i></td>		
					<td class="layui-td-gray">工作类型</td>
					<td class="hover-edit"><span id="cate_{$detail.id}"
							data-val="{$detail.cate}">{$detail.cate_name}</span><i class="iconfont icon-wodedianping"
							title="编辑" data-name="cate"></i></td>	
					<td class="layui-td-gray">优先级</td>
					<td class="hover-edit"><span class="layui-color-{$detail.priority}" id="priority_{$detail.id}"
							data-val="{$detail.priority}">{$detail.priority_name}</span><i
							class="iconfont icon-wodedianping" title="编辑" data-name="priority"></i>
					</td>
				</tr>
				<tr>	
					<td class="layui-td-gray-2">实际完成日期</td>
					<td class="hover-edit">{eq name="$detail.over_time" value="0"}-{else/}{$detail.over_time|
						date='Y-m-d'}{/eq}</td>	
					<td class="layui-td-gray">实际工时</td>
					<td class="hover-edit">{eq name="$detail.work_hours" value="0"}-{else/}{$detail.work_hours}{/eq}
					</td>	
					<td class="layui-td-gray">任务状态</td>
					<td class="hover-edit">
						<span class="layui-color-{$detail.flow_status}" id="flow_status_{$detail.id}"
							data-val="{$detail.flow_status}">{$detail.flow_name}</span><i
							class="iconfont icon-wodedianping" title="编辑" data-name="flow_status"></i>
					</td>					
					<td class="layui-td-gray">完成进度</td>
					<td class="hover-edit"><span class="layui-color-{$detail.done_ratio}" id="done_ratio_{$detail.id}" data-val="{$detail.done_ratio}">{$detail.done_ratio}%</span><i class="iconfont icon-wodedianping" title="编辑" data-name="done_ratio"></i>
					</td>						
				</tr>
			</table>
			<div class="content hover-edit">
				<h3 class="py-4">任务描述<i class="iconfont icon-wodedianping" title="编辑" data-name="content"></i></h3>
				<div class="content-content" id="content_{$detail.id}" data-val="{$detail.content}">
					{$detail.content|raw}</div>
			</div>
			<div class="detail-file">
				<h3 class="py-4">文件附件</h3>
				<div>
					<button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="flieBtn">选择文件并上传</button>
					<span
						style="color:#FF5722; font-size:12px; margin-left: 8px;">上传前，请规范命名，最大只能上传{:get_system_info('upload_max_filesize')}的文件，超过请压缩成多个文件上传。</span>
					<div class="layui-upload-list" {empty name="$file_array" }style="display:none;" {/empty}>
						<table class="layui-table">
							<thead>
								<tr>
									<th width="60%">文件名</th>
									<th width="12%" style="text-align:center">文件大小</th>
									<th width="12%" style="text-align:center">上传人</th>
									<th width="15%" style="text-align:center">操作</th>
								</tr>
							</thead>
							<tbody id="flieList">
								{volist name="file_array" id="vo"}
								<tr>
									<td>{$vo.name}</td>
									<td style="text-align: center;">{$vo.filesize/1048576|round=2}MB</td>
									<td style="text-align: center;">{$vo.admin_name}</td>
									<td style="text-align: center;">
										<div class="layui-btn-group"><a class="layui-btn layui-btn-xs "
												href="{$vo.filepath}" download="{$vo.name}" data-id="{$vo.id}">下载</a>
											<span class="layui-btn layui-btn-xs layui-btn-danger file-delete"
												data-uid="{$vo.admin_id}" data-id="{$vo.id}">删除</span>
										</div>
									</td>
								</tr>
								{/volist}
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
		<div class="layui-col-md3 p-4">
			<div class="pb-4 hover-{$role_edit}">
				<p class="gray mb-2">关联项目</p>
				<span id="project_id_{$detail.id}" data-val="{$detail.project_id}">
					{gt name="$detail.project_id" value="0"}{$detail.project_name}{else/}<span
						class="gray">未设置</span>{/gt}
				</span>
				<i class="iconfont icon-wodedianping" title="编辑" data-name="project_id"></i>
			</div>

			<div class="pb-4 mt-3">
				<h5 class="mb-1">工作记录<span class="layui-btn layui-btn-xs add-schedule right">+ 工作记录</span></h5>
				<div id="schedule_{$detail.id}" style="line-height:1.5">
					<div class="layui-data-none">暂无工作记录</div>
				</div>
			</div>
		</div>
	</div>
	<div class="layui-tab layui-tab-brief px-3" lay-filter="docDemoTabBrief">
		<ul class="layui-tab-title">
			<li class="layui-this">员工评论({$detail.comments})</li>
			<li>操作日志({$detail.logs})</li>
		</ul>
		<div class="layui-tab-content p-0">
			<div class="layui-tab-item comment-list layui-show">
				<div class="comment-input my-2">
					<input type="text" id="commentInput" readonly placeholder="发表一下你的看法" class="layui-input" value="">
				</div>
				<div id="comment_task_{$detail.id}"></div>
			</div>
			<div class="layui-tab-item logs-list">
				<div id="log_task_{$detail.id}"></div>
			</div>
		</div>
	</div>
</div>

{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
	const admin_id = '{$detail.admin_id}';
	const detail_id = '{$detail.id}';
	const moduleInit = ['tool','employeepicker','oaEdit','oaSchedule','oaComment'];
	function gouguInit() {
		const opsData = {
			flow_status: [
				{ 'id': 1, 'title': '未开始' },
				{ 'id': 2, 'title': '进行中' },
				{ 'id': 3, 'title': '已完成' },
				{ 'id': 4, 'title': '不解决' },
				{ 'id': 5, 'title': '已关闭' }
			],
			type: [
				{ 'id': 1, 'title': '需求' },
				{ 'id': 2, 'title': '设计' },
				{ 'id': 3, 'title': '研发' },
				{ 'id': 4, 'title': '缺陷' }
			],
			priority: [
				{ 'id': 1, 'title': '低' },
				{ 'id': 2, 'title': '中' },
				{ 'id': 3, 'title': '高' },
				{ 'id': 4, 'title': '紧急' }
			],
			done_ratio: [
				{ 'id': 0, 'title': '0%' },
				{ 'id': 20, 'title': '20%' },
				{ 'id': 40, 'title': '40%' },
				{ 'id': 50, 'title': '50%' },
				{ 'id': 60, 'title': '60%' },
				{ 'id': 80, 'title': '80%' },
				{ 'id': 100, 'title': '100%' }
			]
		}
		const edit = layui.oaEdit,work = layui.oaSchedule,comment = layui.oaComment, upload = layui.upload, tool = layui.tool;

		$('.hover-edit').hover(function () {
			$(this).addClass('hover-on');
		}, function () {
			$(this).removeClass('hover-on');
		})

		$('.hover-edit').on('click', 'i', function () {
			let name = $(this).data('name');
			let show_txt = $('#' + name + '_' + detail_id).text().replace(/[\r\n\t]/g, "");
			let real_txt = $('#' + name + '_' + detail_id).data('val');
			if (real_txt === '') {
				real_txt = show_txt;
			}
			editShow(detail_id, name, show_txt, real_txt);
		})

		$('#delBtn').on('click', function () {
			layer.confirm('确定要删除该任务吗？请慎重', { icon: 3, title: '提示' }, function (index) {
				let callback = function (e) {
					layer.closeAll();
					layer.msg(e.msg);
					parent.layui.taskTable.reload();
					if (e.code == 0) {
						tool.sideClose(1000);
					}
				}
				let postData = { "id": detail_id };
				tool.delete("/project/task/delete", postData, callback);
			});
		})

		upload.render({
			elem: '#flieBtn'
			, url: '/api/index/upload'
			, accept: 'file' //普通文件
			, exts: 'jpeg|jpg|png|gif|txt|doc|docx|ppt|pptx|xls|xlsx|pdf|zip|rar|7z' //只允许上传文件
			, before: function (obj) {
				layer.msg('上传中...', { time: 3600000 });
			}
			, done: function (res, index, upload) {
				let callback = function (e) {
					layer.msg('上传成功');
					location.reload();
				}
				let postData = { 'topic_id': detail_id, 'file_id': res.data.id, 'file_name': res.data.name, 'module': 'task' };
				if(res.code>0){
					layer.msg(res.msg);
				}
				else{
					tool.post("/project/api/add_file", postData, callback);
				}				
			}
			, error: function (index, upload) {
				layer.msg('上传失败');
			}
		});

		$('#flieList').on('click', '.file-delete', function () {
			let id = $(this).data('id');
			let uid = $(this).data('uid');
			if (uid != login_admin) {
				layer.msg('你不是该文件的上传人，无权限删除');
				return false;
			}
			layer.confirm('确定要删除该附件吗?', { icon: 3, title: '提示' }, function (index) {
				let callback = function (e) {
					layer.closeAll();
					layer.msg(e.msg);
					location.reload();
				}
				let postData = { 'id': id };
				tool.delete("/project/api/delete_file", postData, callback);
			});
		})

		//工作记录相关
		work.load(detail_id);
		$('.add-schedule').on('click', function () {
			work.add(detail_id,{'id':0});	
		})
		
		//任务日志
		comment.log(detail_id,'task');

		//评论
		comment.load(detail_id,'task');
		$('#commentInput').on('click', function () {
			comment.textarea(0,detail_id,0,0,'task','');
		})
		//回复
		$('#comment_task_' + detail_id).on('click', '[data-event="replay"]', function () {
			let pid = $(this).data('id');
			let padmin_id = $(this).data('uid');
			comment.textarea(0, detail_id, pid, padmin_id, 'task', '');
		})
		//编辑
		$('#comment_task_' + detail_id).on('click', '[data-event="edit"]', function () {
			let id = $(this).data('id');
			let content = $('#comment_' + id).data('content');
			comment.textarea(id, detail_id, 0, 0, 'task', content);
		})

		//删除
		$('#comment_task_' + detail_id).on('click', '[data-event="del"]', function () {
			let id = $(this).data('id');
			comment.del(id, detail_id, 'task');
		})

		let loading = false;
		let editPost = function (id, name, show_val, real_val) {
			let callback = function (e) {
				layer.closeAll();
				if (e.code == 0) {
					location.reload();
					if (layui.pageTable) {
						setTimeout(function () {
							layui.pageTable.reload();
						}, 400)
					}
				}
				else {
					layer.msg(e.msg);
				}
			}
			let postData = { id: id };
			postData[name] = real_val;
			tool.post("/project/task/add", postData, callback);
		}

		function editShow(id, name, show_txt, real_txt) {
			if (loading == true) {
				return false;
			}
			if (name == "title" || name == "plan_hours") {
				edit.text(id, name, real_txt, editPost);
			}
			if (name == "end_time") {
				if (admin_id == login_admin) {
					edit.date(id, name, real_txt, editPost);
				} else {
					layer.msg('您没权限修改，请联系创建人修改');
				}
			}
			if (name == "director_uid") {
				edit.employee_one(id, name, show_txt, real_txt, editPost);
			}
			if (name == "assist_admin_ids") {
				edit.employee_more(id, name, show_txt, real_txt, editPost);
			}
			if (name == "flow_status" || name == "priority" || name == "done_ratio" || name == "type") {
				edit.dropdown(id, name, real_txt, opsData[name], editPost);
			}
			if (name == "cate") {
				loading = true;
				tool.get("/api/index/get_work_cate", {}, function (res) {
					let data = res.data;
					loading = false;
					edit.dropdown(id, name, real_txt, data, editPost, 0);
				});
			}
			if (name == "project_id") {
				if (admin_id == login_admin) {
					edit.select_table(id, name, real_txt, "/project/api/get_project", editPost, 1);
				} else {
					layer.msg('您没权限修改，请联系创建人或负责人修改');
				}
			}
			if (name == "content") {
				edit.editor(id, name, real_txt, editPost);
			}
		}
	}
</script>
{/block}